package Dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import common.Dbutil;
import vo.User;

public class Userdao {
	private User user;//用户信息
	private Dbutil db;
	public Userdao(){
		
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Dbutil getDb() {
		return db;
	}
	public void setDb(Dbutil db) {
		this.db = db;
	}
	public Userdao(User user){
		this.setUser(user);
	}
	/**
	 * 注册
	 * @param user
	 * @return -1失败 >=0成功
	 */
	public int do_register(User user){
		int result = -1;
		db = new Dbutil();
		String sql = "insert into userinfo(username,password,email,gender,realname,phone) values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getEmail()+"','"+user.getGender()+"','"+user.getRealname()+"','"+user.getPhone()+"');";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 注册
	 * @param 
	 * @return -1失败 >=0成功
	 */
	public int do_register(){
		int result = -1;
		db = new Dbutil();
		String sql = "insert into userinfo(username,password,email,gender,realname,phone) values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getEmail()+"','"+user.getGender()+"','"+user.getRealname()+"','"+user.getPhone()+"');";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 修改
	 * @param user
	 * @return -1失败 >=0成功
	 */
	public int do_change(User user){
		int result = -1;
		db = new Dbutil();
		String sql = "update userinfo set username='" +user.getUsername() + "',email='" +user.getEmail() + "',gender='" +user.getGender() + "',phone='" +user.getPhone() + "' where realname='" +user.getRealname()+"';";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 修改
	 * @param user
	 * @return -1失败 >=0成功
	 */
	public int do_change(String id,User user){
		int result = -1;
		db = new Dbutil();
		String sql = "update userinfo set username='" +user.getUsername() + "',email='" +user.getEmail() + "',gender='" +user.getGender() + "',phone='" +user.getPhone() + "' where user_id=" +id+ ";";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 验证登陆密码账号是否正确
	 * @param uname
	 * @param pass
	 * @return
	 */
	public boolean isLogin(String email,String password){
		boolean result =false;
		db = new Dbutil();
		String sql = "select username from userinfo where email='"+ email +"' AND password='" + password + "';";;
		ResultSet rs = db.executeQuery(sql);
		try {
			result = rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		db.closeDB();
		return result;	
	}
	/**
	 * 根据用户名得到完整的用户信息
	 * @param Uname
	 * @retuen 用户对象
	 */
	public User getUserByUname(String email){
		User user = null;
		db = new Dbutil();
		String sql = "select user_id,username,password,gender,realname,phone,user_type from userinfo where email='" + email + "';";
		ResultSet rs = db.executeQuery(sql);
		try {
			if(rs.next()){
				user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setGender(rs.getString("gender"));
				user.setRealname(rs.getString("realname"));
				user.setPhone(rs.getString("phone"));
				user.setUser_type(rs.getString("user_type"));
				user.setEmail(email);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return user;
		
	}
	/**
	 * 根据id得到完整的用户信息
	 * @param Uname
	 * @retuen 用户对象
	 */
	public User getUserByUname_id(String id){
		User user = null;
		db = new Dbutil();
		String sql = "select user_id,username,password,gender,realname,phone,user_type,email from userinfo where user_id='" + id + "';";
		ResultSet rs = db.executeQuery(sql);
		try {
			if(rs.next()){
				user = new User();
				user.setUser_id(rs.getInt("user_id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setGender(rs.getString("gender"));
				user.setRealname(rs.getString("realname"));
				user.setPhone(rs.getString("phone"));
				user.setUser_type(rs.getString("user_type"));
				user.setEmail(rs.getString("email"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return user;
		
	}
	/**
	 * 判断是否存在该用户(重写)
	 * @param User
	 * @return true 存在 false 不存在
	 */
	public boolean isExitUname(User user){
		boolean result = true;
		//算法
		db = new Dbutil();
		String sql = "select email from userinfo where email='"+user.getEmail()+"';";
		ResultSet rs = db.executeQuery(sql);
		try {
			result = rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		db.closeDB();
		return result;
		
	}
	/**
	 * 判断是否存在该用户(重写)
	 * @param User
	 * @return true 存在 false 不存在
	 */
	public boolean isExitUname_user(User user){
		boolean result = true;
		//算法
		db = new Dbutil();
		String sql = "select email,user_id from userinfo where email='"+user.getEmail()+"';";
		ResultSet rs = db.executeQuery(sql);
		try {
			result = rs.next();
			int id = rs.getInt("user_id");
			if(id==user.getUser_id()){
				result = false;
			}
			System.out.print(id+" "+user.getRealname());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		db.closeDB();
		return result;
		
	}
	/**
	 * 判断是否存在该用户(重写)
	 * @param User
	 * @return true 存在 false 不存在
	 */
	public boolean isExitUname_user(String email,int id2){
		boolean result = true;
		//算法
		db = new Dbutil();
		String sql = "select email,user_id from userinfo where email='"+email+"';";
		ResultSet rs = db.executeQuery(sql);
		try {
			result = rs.next();
			int id = rs.getInt("user_id");
			if(id==id2){
				result = false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		db.closeDB();
		return result;
		
	}
	/**
	 * 查询
	 * @param uname
	 */
	public ArrayList<User> do_SelectByUname(String username){
		ArrayList<User> list = new ArrayList<User>();
		db = new Dbutil();
		String sql = "select user_id,username,realname,gender,email,phone from userinfo where username like '%"+username+"%';";
		ResultSet rs = db.executeQuery(sql);
		try {
			while(rs.next()){
			User user = new User();
			user.setUser_id(rs.getInt("user_id"));
			user.setUsername(rs.getString("username"));
			user.setRealname(rs.getString("realname"));
			user.setGender(rs.getString("gender"));
			user.setEmail(rs.getString("email"));
			user.setPhone(rs.getString("phone"));
			list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return list;
	}
	public ArrayList<User> do_SelectByUname(String username,int thispage){
		ArrayList<User> list = new ArrayList<User>();
		db = new Dbutil();
		String sql = "select user_id,username,realname,gender,email,phone from userinfo where username like '%"+username+"%' limit "+thispage+",4 ;";
		ResultSet rs = db.executeQuery(sql);
		try {
			while(rs.next()){
			User user = new User();
			user.setUser_id(rs.getInt("user_id"));
			user.setUsername(rs.getString("username"));
			user.setRealname(rs.getString("realname"));
			user.setGender(rs.getString("gender"));
			user.setEmail(rs.getString("email"));
			user.setPhone(rs.getString("phone"));
			list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.closeDB();
		return list;
	}
	/**
	 * 删除用户
	 * @param id
	 * @return 
	 */
	public int user_delete(String id){
		int result = -1;
		db = new Dbutil();
		String sql = "delete from userinfo where user_id='"+id+"';";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}
	/**
	 * 通过id得到pass
	 * @param id
	 * @return
	 */
	public String getpass(int id){
		String pass = "";
		db = new Dbutil();
		String sql = "select password from userinfo where user_id = "+id+";";
		ResultSet rs = db.executeQuery(sql);
		try {
			if(rs.next()){
				pass = rs.getString("password");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		db.closeDB();
		return pass;
	}
	public int setpass(int id,String pass){
		int result = -1;
		db = new Dbutil();
		String sql = "update userinfo set password = '"+pass+"' where user_id = "+id+"; ";
		result = db.executeUpdate(sql);
		db.closeDB();
		return result;
	}

}
